package com.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.constant.SystemConstants;
import com.my.domain.entity.Repair;
import com.my.domain.vo.TempVO;
import com.my.enums.ResultEnum;
import com.my.mapper.RepairMapper;
import com.my.result.Result;
import com.my.service.RepairService;
import com.my.utils.AccountProfile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 维修表(Repair)表服务实现类
 *
 * @author makejava
 * @since 2022-04-11 13:27:28
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    @Resource
    RepairMapper repairMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Result getRepairList(Integer currentPage) {
        Object o = redisTemplate.opsForValue().get("Repair:List"+currentPage);
        if(o==null) {
            Subject subject = SecurityUtils.getSubject();
            AccountProfile currentUser = (AccountProfile) subject.getPrincipal();
            LambdaQueryWrapper<Repair> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Repair::getDelFlag, SystemConstants.REPAIR_STATUS_NORMAL);
            lambdaQueryWrapper.eq(Repair::getUserId, currentUser.getId());
            lambdaQueryWrapper.orderByAsc(Repair::getRepairTime);
            Page<Repair> page = new Page<>(currentPage, 5);
            page(page, lambdaQueryWrapper);
            IPage pageData = page(page, lambdaQueryWrapper);
            redisTemplate.opsForValue().set("Repair:List"+currentPage,pageData);
            return Result.okResult(pageData);
        }
            IPage pageData = (IPage) o;
            return Result.okResult(pageData);


    }


    @Override
    public Result getRepairByTime() {
        LambdaQueryWrapper<Repair> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Repair::getDelFlag, SystemConstants.REPAIR_STATUS_NORMAL);
        lambdaQueryWrapper.orderByAsc(Repair::getRepairTime);
        Page<Repair>page = new Page<>(1,5);
        page(page,lambdaQueryWrapper);
        List<Repair> repairs = page.getRecords();

        return Result.okResult(repairs);
    }


    @Override
    public Result deleteRepairById(Long id) {

        if(id!=null){
            Repair repair = new Repair();
            repair.setId(id);
            repair.setDelFlag(0);
            repairMapper.updateById(repair);
            //removeById(id);
        }
        else
            return Result.errorResult(ResultEnum.DELETE_FAIL);

        return Result.okResult(ResultEnum.DELETE_SUCCESS);
    }

    @Override
    public Result addRepair(TempVO temp) {
        Repair repair = new Repair();

        repair.setId(temp.getId());
        repair.setRepairType(temp.getRepairType());
        repair.setRepairDetail(temp.getRepairDetail());
        if(temp.getId()!=null){
            repairMapper.updateById(repair);
            return Result.okResult(ResultEnum.UPDATE_SUCCESS);
        }
        //SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        repair.setRepairTime(date);
        repair.setRepairState(0);
        repair.setRepairPeopleid(1l);//先设置默认唯1，因为我不能分配
        repair.setUserId(1l);//因为还没搞token获取不到当前用户，先默认
        save(repair);
        return Result.okResult(ResultEnum.ADD_SUCCESS);
    }
}
